import json
from .usart_config import ser


def send_car_data(str):
    data = bytes(str, 'utf-8')
    ser.flushInput()
    ser.write(data)



def get_car_data():
    while True:  # 获取状态数据
        size = ser.inWaiting()
        if size != 0:
            break
    res_data = ser.read(size)
    # print(f"res_data: {res_data}")
    if res_data == b'\x00':
        print('暂未接受到数据')
        return '-1', '-1', '-1', '-1'
    try:
        res_dic = json.loads(res_data)
    except:
        print("json数据转换失败")
        return '-2', '-2', '-2', '-2'

    # print(res_data)
    
    point = res_dic['point']  # '116.322283,39.867492'
    i_temp = res_dic['temp']
    i_hum = res_dic['hum']
    warning_flag = res_dic['warning'] # 0表示无异常
    return point, i_temp, i_hum, warning_flag

